import unittest
import uuid
import json

from Autotests.BaseTest import BaseTestCase
from Autotests.Cloud.Clients import FinancialService


basePriceData = {
        "endCustomerSizeThresholds": {
            "Medium": 2.0,
            "Regular": 1.0,
            "Small": 0.50
        },
        "listType": "Weight-based",
        "prices": {
            "Distributor": {
                "AllInclusive": {
                    "Bronze": {
                        "Large": 0.3150,
                        "Medium": 0.360,
                        "Regular": 0.4050,
                        "Small": 0.450
                    },
                    "Gold": {
                        "Large": 0.2450,
                        "Medium": 0.280,
                        "Regular": 0.3150,
                        "Small": 0.350
                    },
                    "Registered": {
                        "Large": 0.350,
                        "Medium": 0.40,
                        "Regular": 0.450,
                        "Small": 0.50
                    },
                    "Silver": {
                        "Large": 0.280,
                        "Medium": 0.320,
                        "Regular": 0.360,
                        "Small": 0.40
                    }
                },
                "SoftwareOnly": {
                    "Bronze": {
                        "Large": 0.3150,
                        "Medium": 0.360,
                        "Regular": 0.4050,
                        "Small": 0.450
                    },
                    "Gold": {
                        "Large": 0.2460,
                        "Medium": 0.280,
                        "Regular": 0.3150,
                        "Small": 0.350
                    },
                    "Registered": {
                        "Large": 0.350,
                        "Medium": 0.40,
                        "Regular": 0.450,
                        "Small": 0.50
                    },
                    "Silver": {
                        "Large": 0.280,
                        "Medium": 0.320,
                        "Regular": 0.360,
                        "Small": 0.40
                    }
                }
            },
            "EndCustomer": {
                "AllInclusive": {
                    "Bronze": {
                        "Large": 0.3150,
                        "Medium": 0.360,
                        "Regular": 0.4050,
                        "Small": 0.450
                    },
                    "Gold": {
                        "Large": 0.2450,
                        "Medium": 0.280,
                        "Regular": 0.3150,
                        "Small": 0.350
                    },
                    "Registered": {
                        "Large": 0.350,
                        "Medium": 0.40,
                        "Regular": 0.450,
                        "Small": 0.50
                    },
                    "Silver": {
                        "Large": 0.280,
                        "Medium": 0.320,
                        "Regular": 0.360,
                        "Small": 0.40
                    }
                },
                "SoftwareOnly": {
                    "Bronze": {
                        "Large": 0.3150,
                        "Medium": 0.360,
                        "Regular": 0.4050,
                        "Small": 0.450
                    },
                    "Gold": {
                        "Large": 0.2450,
                        "Medium": 0.280,
                        "Regular": 0.3150,
                        "Small": 0.350
                    },
                    "Registered": {
                        "Large": 0.350,
                        "Medium": 0.40,
                        "Regular": 0.450,
                        "Small": 0.50
                    },
                    "Silver": {
                        "Large": 0.280,
                        "Medium": 0.320,
                        "Regular": 0.360,
                        "Small": 0.40
                    }
                }
            },
            "Reseller": {
                "AllInclusive": {
                    "Bronze": {
                        "Large": 0.3150,
                        "Medium": 0.360,
                        "Regular": 0.4050,
                        "Small": 0.450
                    },
                    "Gold": {
                        "Large": 0.2450,
                        "Medium": 0.280,
                        "Regular": 0.3150,
                        "Small": 0.350
                    },
                    "Registered": {
                        "Large": 0.350,
                        "Medium": 0.40,
                        "Regular": 0.450,
                        "Small": 0.50
                    },
                    "Silver": {
                        "Large": 0.280,
                        "Medium": 0.320,
                        "Regular": 0.360,
                        "Small": 0.40
                    }
                },
                "SoftwareOnly": {
                    "Bronze": {
                        "Large": 0.3150,
                        "Medium": 0.360,
                        "Regular": 0.4050,
                        "Small": 0.450
                    },
                    "Gold": {
                        "Large": 0.2450,
                        "Medium": 0.280,
                        "Regular": 0.3150,
                        "Small": 0.350
                    },
                    "Registered": {
                        "Large": 0.350,
                        "Medium": 0.40,
                        "Regular": 0.450,
                        "Small": 0.50
                    },
                    "Silver": {
                        "Large": 0.280,
                        "Medium": 0.320,
                        "Regular": 0.360,
                        "Small": 0.40
                    }
                }
            },
            "SubDistributor": {
                "AllInclusive": {
                    "Bronze": {
                        "Large": 0.3150,
                        "Medium": 0.360,
                        "Regular": 0.4050,
                        "Small": 0.450
                    },
                    "Gold": {
                        "Large": 0.2450,
                        "Medium": 0.280,
                        "Regular": 0.3150,
                        "Small": 0.350
                    },
                    "Registered": {
                        "Large": 0.350,
                        "Medium": 0.40,
                        "Regular": 0.450,
                        "Small": 0.50
                    },
                    "Silver": {
                        "Large": 0.280,
                        "Medium": 0.320,
                        "Regular": 0.360,
                        "Small": 0.40
                    }
                },
                "SoftwareOnly": {
                    "Bronze": {
                        "Large": 0.3150,
                        "Medium": 0.360,
                        "Regular": 0.4050,
                        "Small": 0.450
                    },
                    "Gold": {
                        "Large": 0.2450,
                        "Medium": 0.280,
                        "Regular": 0.3150,
                        "Small": 0.350
                    },
                    "Registered": {
                        "Large": 0.350,
                        "Medium": 0.40,
                        "Regular": 0.450,
                        "Small": 0.50
                    },
                    "Silver": {
                        "Large": 0.280,
                        "Medium": 0.320,
                        "Regular": 0.360,
                        "Small": 0.40
                    }
                }
            }
        },
        "resellerWeightThresholds": {
            "Bronze": 2.0,
            "Registered": 1.0,
            "Silver": 3.0
        },
        "variousDiscounts": {
            "Charity": 50.0,
            "Education": 50.0,
            "Workstation": 50.0
        },
        "volumeType": "UsedStorage"
    }

newPriceData = {
        "endCustomerSizeThresholds": {
            "Medium": 2.1,
            "Regular": 1.1,
            "Small": 0.52
        },
        "listType": "Weight-based",
        "prices": {
            "Distributor": {
                "AllInclusive": {
                    "Bronze": {
                        "Large": 0.3250,
                        "Medium": 0.340,
                        "Regular": 0.4150,
                        "Small": 0.480
                    },
                    "Gold": {
                        "Large": 0.2250,
                        "Medium": 0.270,
                        "Regular": 0.3350,
                        "Small": 0.370
                    },
                    "Registered": {
                        "Large": 0.360,
                        "Medium": 0.48,
                        "Regular": 0.430,
                        "Small": 0.52
                    },
                    "Silver": {
                        "Large": 0.290,
                        "Medium": 0.330,
                        "Regular": 0.370,
                        "Small": 0.42
                    }
                },
                "SoftwareOnly": {
                    "Bronze": {
                        "Large": 0.3250,
                        "Medium": 0.370,
                        "Regular": 0.4150,
                        "Small": 0.454
                    },
                    "Gold": {
                        "Large": 0.2468,
                        "Medium": 0.288,
                        "Regular": 0.3158,
                        "Small": 0.356
                    },
                    "Registered": {
                        "Large": 0.350,
                        "Medium": 0.40,
                        "Regular": 0.450,
                        "Small": 0.52
                    },
                    "Silver": {
                        "Large": 0.260,
                        "Medium": 0.360,
                        "Regular": 0.380,
                        "Small": 0.42
                    }
                }
            },
            "EndCustomer": {
                "AllInclusive": {
                    "Bronze": {
                        "Large": 0.3250,
                        "Medium": 0.380,
                        "Regular": 0.4060,
                        "Small": 0.454
                    },
                    "Gold": {
                        "Large": 0.2452,
                        "Medium": 0.286,
                        "Regular": 0.3158,
                        "Small": 0.352
                    },
                    "Registered": {
                        "Large": 0.354,
                        "Medium": 0.44,
                        "Regular": 0.454,
                        "Small": 0.56
                    },
                    "Silver": {
                        "Large": 0.282,
                        "Medium": 0.322,
                        "Regular": 0.362,
                        "Small": 0.46
                    }
                },
                "SoftwareOnly": {
                    "Bronze": {
                        "Large": 0.3250,
                        "Medium": 0.340,
                        "Regular": 0.4450,
                        "Small": 0.460
                    },
                    "Gold": {
                        "Large": 0.2452,
                        "Medium": 0.282,
                        "Regular": 0.3152,
                        "Small": 0.358
                    },
                    "Registered": {
                        "Large": 0.360,
                        "Medium": 0.42,
                        "Regular": 0.420,
                        "Small": 0.56
                    },
                    "Silver": {
                        "Large": 0.282,
                        "Medium": 0.322,
                        "Regular": 0.362,
                        "Small": 0.48
                    }
                }
            },
            "Reseller": {
                "AllInclusive": {
                    "Bronze": {
                        "Large": 0.3150,
                        "Medium": 0.360,
                        "Regular": 0.4050,
                        "Small": 0.450
                    },
                    "Gold": {
                        "Large": 0.2450,
                        "Medium": 0.280,
                        "Regular": 0.3150,
                        "Small": 0.350
                    },
                    "Registered": {
                        "Large": 0.350,
                        "Medium": 0.40,
                        "Regular": 0.450,
                        "Small": 0.50
                    },
                    "Silver": {
                        "Large": 0.280,
                        "Medium": 0.320,
                        "Regular": 0.360,
                        "Small": 0.40
                    }
                },
                "SoftwareOnly": {
                    "Bronze": {
                        "Large": 0.3150,
                        "Medium": 0.360,
                        "Regular": 0.4050,
                        "Small": 0.450
                    },
                    "Gold": {
                        "Large": 0.2450,
                        "Medium": 0.280,
                        "Regular": 0.3150,
                        "Small": 0.350
                    },
                    "Registered": {
                        "Large": 0.350,
                        "Medium": 0.40,
                        "Regular": 0.450,
                        "Small": 0.50
                    },
                    "Silver": {
                        "Large": 0.280,
                        "Medium": 0.320,
                        "Regular": 0.360,
                        "Small": 0.40
                    }
                }
            },
            "SubDistributor": {
                "AllInclusive": {
                    "Bronze": {
                        "Large": 0.3150,
                        "Medium": 0.360,
                        "Regular": 0.4050,
                        "Small": 0.450
                    },
                    "Gold": {
                        "Large": 0.2450,
                        "Medium": 0.280,
                        "Regular": 0.3150,
                        "Small": 0.350
                    },
                    "Registered": {
                        "Large": 0.350,
                        "Medium": 0.40,
                        "Regular": 0.450,
                        "Small": 0.50
                    },
                    "Silver": {
                        "Large": 0.280,
                        "Medium": 0.320,
                        "Regular": 0.360,
                        "Small": 0.40
                    }
                },
                "SoftwareOnly": {
                    "Bronze": {
                        "Large": 0.3150,
                        "Medium": 0.360,
                        "Regular": 0.4050,
                        "Small": 0.450
                    },
                    "Gold": {
                        "Large": 0.2450,
                        "Medium": 0.280,
                        "Regular": 0.3150,
                        "Small": 0.350
                    },
                    "Registered": {
                        "Large": 0.358,
                        "Medium": 0.48,
                        "Regular": 0.456,
                        "Small": 0.54
                    },
                    "Silver": {
                        "Large": 0.286,
                        "Medium": 0.324,
                        "Regular": 0.320,
                        "Small": 0.44
                    }
                }
            }
        },
        "resellerWeightThresholds": {
            "Bronze": 2.2,
            "Registered": 1.4,
            "Silver": 3.6
        },
        "variousDiscounts": {
            "Charity": 40.0,
            "Education": 40.0,
            "Workstation": 40.0
        },
        "volumeType": "UsedStorage"
    }

class PriceListsTest(BaseTestCase):
    def test_PriceLists(self):
        finserv = FinancialService()

        def TestPriceInfo(priceEntityId, name, currency, billingMethod, priceData):
            r = finserv.createObject('GetPriceInfoRequest')
            r.priceEntityId = priceEntityId

            getPriceInfoResult = finserv.call('GetPriceInfo', r)
            self.assertEqual(getPriceInfoResult.priceInfo.descriptor.name, name)
            self.assertEqual(getPriceInfoResult.priceInfo.descriptor.currency, currency)
            self.assertEqual(getPriceInfoResult.priceInfo.billingMethod, billingMethod)
            self.assertEqual(json.loads(getPriceInfoResult.priceInfo.priceData), priceData)

        name = 'test-' + str(uuid.uuid4())
        currency = 'EUR'
        billingMethod = 'VolumeBased'

        r = finserv.createObject('AddPriceRequest')
        r.priceInfo.descriptor.name = name
        r.priceInfo.descriptor.currency = currency
        r.priceInfo.billingMethod = billingMethod
        r.priceInfo.priceData = json.dumps(basePriceData)

        addPriceResult = finserv.call('AddPrice', r)

        TestPriceInfo(addPriceResult.key.entityId, name, currency, billingMethod, basePriceData)

        r = finserv.createObject('EditPriceRequest')
        r.priceListId = addPriceResult.key.entityId
        r.priceInfo.descriptor.name = name
        r.priceInfo.descriptor.currency = currency
        r.priceInfo.billingMethod = billingMethod
        r.priceInfo.priceData = json.dumps(newPriceData)

        editPriceResult = finserv.call('EditPrice', r)

        TestPriceInfo(editPriceResult.key.entityId, name, currency, billingMethod, newPriceData)

        r = finserv.createObject('EnumeratePricesRequest')
        r.billingMethod.value = 'VolumeBased'

        result = finserv.call('EnumeratePrices', r)

if __name__ == "__main__":
    unittest.main()
